﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

namespace SmartProject.Entity
{
    /// <summary>
    /// The Profile class is used to store user specific preference.
    /// </summary>
    public class Profile : EntityBase
    {
        /// <summary>
        /// Gets or sets the name of the Profile.
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// Gets or sets the value of the Profile.
        /// </summary>
        public virtual string Value { get; set; }

        /// <summary>
        /// Gets or sets the Owner user of the Profile.
        /// </summary>
        public virtual User Owner { get; set; }

        /// <summary>
        /// Gets a specific user profile.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="ownerId">Owner user Id.</param>
        /// <param name="name">Name of the profile to get.</param>
        /// <returns>Profile.</returns>
        public static Profile GetUserProfile(ISession session, int ownerId, string name)
        {
            IList<ICriterion> expressions = new List<ICriterion>();
            expressions.Add(Restrictions.Eq("Owner", User.GetUser(session, ownerId)));
            expressions.Add(Restrictions.Eq("Name", name));
            IList<EntityBase> entities = Find(session, expressions, null, typeof(Profile));
            if (entities != null && entities.Count == 1)
            {
                return (Profile)entities[0];
            }
            return null;
        }
    }
}
